﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Experimental.Rendering.Universal;

public enum HunterState
{
    over,
    idle,
    look,
    attack,
    patrol
}

public class Hunter_InHouse : HunterBase
{

    Vector2 target;
    HunterState currentState;

    [SerializeField] private float moveSpeed;
    public List<Transform> prtrolPointList;
    int nextPointIndex = 0;
    Transform currentPoint;
    
    float stayTime = 2.0f;
    bool m_Patroling;
    bool m_isOnPoint;

    Transform player;


    private void Start()
    {
        if (prtrolPointList == null)
            prtrolPointList = new List<Transform>();

        currentState = HunterState.idle;        
        currentPoint = gameObject.transform;
        m_Patroling = true;
        m_isOnPoint = true;
        StartCoroutine(PatrolOnPath());
    }

    private void Update()
    {
        CheckChangeState();
    }

    private void FixedUpdate()
    {
        CheckState(currentState);
        //Walk(movement);
    }

    void NormalPatrol()
    {      
        Walk(target,moveSpeed);
    }


    /// <summary>
    /// 交换巡逻点
    /// </summary>
    void ChangePatrolPoint()
    {
        if (prtrolPointList.Count > 0)
        {
            currentPoint = prtrolPointList[nextPointIndex];
            m_isOnPoint = false;
            nextPointIndex++;
            if (nextPointIndex >= prtrolPointList.Count)
                nextPointIndex = 0;
        }      
    }

    IEnumerator PatrolOnPath()
    {
        if (m_isOnPoint.Equals(null))
            yield break;
        
        while (m_Patroling)
        {
            //Debug.Log(currentState);
            if (m_isOnPoint == true)
            {                
                ChangePatrolPoint();
                yield return new WaitForSecondsRealtime(stayTime);
                currentState = HunterState.patrol;
            }
            yield return new WaitForFixedUpdate();
        }
        yield break;
    }

    /// <summary>
    /// 改变猎人状态
    /// </summary>
    void CheckChangeState()
    {
        if (currentState == HunterState.attack)
            return;
        if (Mathf.Abs(transform.position.x - currentPoint.position.x) < 0.5)
        {
            m_isOnPoint = true;
            currentState = HunterState.idle;            
        }   
        if(Mathf.Abs(transform.position.x - currentPoint.position.x) > 0.5)
        {
            m_isOnPoint = false;
        }
    }

    /// <summary>
    /// 检查猎人当前状态，并执行相关函数
    /// </summary>
    /// <param name="state"></param>
    void CheckState(HunterState state)
    {
        switch (state)
        {
            case HunterState.over:
                break;
            case HunterState.idle:
                Idle();
                
                break;
            case HunterState.look:
                break;
            case HunterState.attack:
                m_Patroling = false;
                Attack(player);
                break;
            case HunterState.patrol:
                target =currentPoint.position;
                NormalPatrol();
                CheckUpStair(currentPoint.transform.position);
                break;
            default:
                break;
        }
    }

    public void StopAttack()
    {      
        currentState = HunterState.idle;
        m_Patroling = true;
    }

    public void StartPatrol()
    {
        StartCoroutine(PatrolOnPath());
        currentState = HunterState.patrol;      
    }

    private void OnTriggerStay2D(Collider2D collision)
    {
        if (collision.tag==TagAndLayersConfigs.Tag.Player)
        {
            currentState = HunterState.attack;
            player = collision.transform;
        }
    }

}
